package com.ys.service.business.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.business.ThrOperateCompanyVps;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.business.*;
import com.ys.service.business.ThrOperateCompanyVpsService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 运营公司表VPS信息
 */
@Service("thrOperateCompanyVpsService")
public class ThrOperateCompanyVpsServiceImpl implements ThrOperateCompanyVpsService {
	private static final Logger logger = LoggerFactory.getLogger(ThrOperateCompanyVpsServiceImpl.class);
	
	@Autowired
	private ThrOperateCompanyVpsMapper thrOperateCompanyVpsMapper;


	@Autowired
	private CheckUtil checkUtil;


	//删除
	public void del(long id) throws Exception{
		thrOperateCompanyVpsMapper.deleteTrue(id);
	}


	/**
	 * 新增
	 */
	@Override
	public R add(Map<String, Object> params, Long menuId) throws Exception {
		logger.info("新增传参："+params);
		//参数验证
		String errorMsg = checkParams(params,menuId);
		if(errorMsg!=null && !"".equals(errorMsg)){
			return R.error(errorMsg);
		}
		ThrOperateCompanyVps thrOperateCompanyVps = JSON.parseObject(JSON.toJSONString(params), ThrOperateCompanyVps.class);
		thrOperateCompanyVps.setStatus(1);
		thrOperateCompanyVpsMapper.insert(thrOperateCompanyVps);
		//return R.ok(EditDto.closeAndReload());
		return R.ok(AddDto.gotoEdit("/web/business/thrOperateCompanyVps/toEdit?id=" + thrOperateCompanyVps.getId()));
	}


	/**
	 * 修改
	 */
	@Override
	public R update(Map<String, Object> params, Long menuId) throws Exception {
		logger.info("修改传参："+params);
		//参数验证
		String errorMsg = checkParams(params,menuId);
		if(errorMsg!=null && !"".equals(errorMsg)){
			return R.error(errorMsg);
		}
		ThrOperateCompanyVps thrOperateCompanyVps = JSON.parseObject(JSON.toJSONString(params), ThrOperateCompanyVps.class);

		thrOperateCompanyVpsMapper.update(thrOperateCompanyVps);
		//return R.ok();
		return R.ok(EditDto.closeAndReload());
	}



	/**
	 * 参数验证
	 */
	public String checkParams(Map<String, Object> params, Long menuId) throws Exception {
		//获取pc字段数据
		List<YsCol> listField = new YsCol()
				.where("A.MenuId = "+menuId)
				.setOrderby("A.SortNo ASC")
				.queryList();
		//验证必填字段的非空；其他字段的长度限制、类型
		String backMsg = checkUtil.checkTableColLengthAndDataType("THR_OperateCompanyVps",listField,params,ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1);
		return backMsg;
	}



}
